
Imports OpenNETCF.Telephony

Public Class frmFlightMode
    Inherits System.Windows.Forms.Form
    Friend WithEvents lbMessages As System.Windows.Forms.ListBox
    Friend WithEvents cbFlightMode As System.Windows.Forms.CheckBox
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

    Friend WithEvents TapiObj As Telephony
    Friend thisLine As Line

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.lbMessages = New System.Windows.Forms.ListBox
        Me.cbFlightMode = New System.Windows.Forms.CheckBox
        '
        'lbMessages
        '
        Me.lbMessages.Location = New System.Drawing.Point(8, 40)
        Me.lbMessages.Size = New System.Drawing.Size(224, 198)
        '
        'cbFlightMode
        '
        Me.cbFlightMode.Location = New System.Drawing.Point(8, 8)
        Me.cbFlightMode.Size = New System.Drawing.Size(88, 20)
        Me.cbFlightMode.Text = "Flight mode"
        '
        'frmFlightMode
        '
        Me.Controls.Add(Me.cbFlightMode)
        Me.Controls.Add(Me.lbMessages)
        Me.Menu = Me.MainMenu1
        Me.MinimizeBox = False
        Me.Text = "Flight mode demo"

    End Sub

#End Region

    Private Sub frmFlightMode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TapiObj = New Telephony
        TapiObj.Initialize()
        AddHandler TapiObj.LineMessage, AddressOf OnLineMessage
        thisLine = FindCellularLine()
        If thisLine Is Nothing Then
            MessageBox.Show("Could not detect cellular line")
            Close()
        End If
    End Sub

    Private Sub cbFlightMode_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbFlightMode.CheckStateChanged
        Dim newState As Integer = CType(IIf(cbFlightMode.Checked, EquipmentState.Minimum, EquipmentState.Full), Integer)
        NativeMethods.lineSetEquipmentState(thisLine.hLine, newState)
        If newState = EquipmentState.Full Then
            NativeMethods.lineRegister(thisLine.hLine, LineRegMode.Automatic, Nothing, 0)
        End If
    End Sub

    Private Sub OnLineMessage(ByVal msg As LineMessage)
        lbMessages.SelectedIndex = lbMessages.Items.Add(msg.ToString())
    End Sub


    Private Function FindCellularLine() As Line
        Dim CellularLine As Line
        Dim numLines As Integer = TapiObj.NumberOfDevices
        For i As Integer = 0 To numLines - 1
            Try
                Dim dc As New DeviceCapabilities(1024)
                dc.Store()
                Dim dwVersion As Integer = TapiObj.NegotiateVersion(i)
                NativeMethods.lineGetDevCaps(TapiObj.hLineApp, i, dwVersion, 0, dc.Data)
                dc.Load()
                If dc.LineName = NativeMethods.CELLTSP_LINENAME_STRING Then
                    CellularLine = TapiObj.CreateLine(i, MediaMode.InteractiveVoice, CallPrivilege.Owner Or CallPrivilege.Monitor)
                    Return CellularLine
                End If
            Catch e As TelephonyException
            End Try
        Next

        Return Nothing
    End Function

    Private Sub frmFlightMode_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        If Not thisLine Is Nothing Then
            thisLine.Dispose()
        End If
        TapiObj.Shutdown()
    End Sub
End Class
